package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
//    private RedisTemplate<Object,Object> redisTemplate;
    private RedisCache redisCache;
    //定义redis缓存所有护理等级的key
    public static final String CACHE_NURSING_PROJECT = "nursingProject:all";

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        deleteCache();
        return save(nursingProject) ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        deleteCache();
        return updateById(nursingProject) ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        deleteCache();
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        deleteCache();
        return removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {
        // 1.先查询缓存数据所有护理等级
//        List<NursingProjectVo> nursingProjectVoList = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        List<NursingProjectVo> nursingProjectVoList = redisCache.getCacheObject(CACHE_NURSING_PROJECT);

        // 2.判断查询的缓存数据是否有效，如果有效直接返回
        if ( CollectionUtil.isNotEmpty(nursingProjectVoList)){
            return nursingProjectVoList;
        }
        // 3.如果缓存数据无效，执行数据库操作查询所有启用的等级
        nursingProjectVoList = nursingProjectMapper.selectAll();
        // 4.将数据库查到的所有等级写入缓存
//        redisTemplate.opsForValue().set(CACHE_NURSING_PROJECT,nursingProjectVoList);
        redisCache.setCacheObject(CACHE_NURSING_PROJECT, nursingProjectVoList);
        // 5.返回数据
        return nursingProjectVoList;
    }


    /**
     * 删除缓存
     */
    private void deleteCache(){
//        redisTemplate.delete(CACHE_NURSING_PROJECT);
        redisCache.deleteObject(CACHE_NURSING_PROJECT);
    }
}
